<?php

require_once dirname(__DIR__) . '/vendor/autoload.php';

use KingBes\Thread\Thread;

class A
{
    public function demo()
    {
        echo "is demo";
    }
}

$class_a = new A();
$a = 1;
// 实例化 线程类
$thread = new Thread();
var_dump("start");
// 执行任务1
$thread->spawn(function () use (&$a) {
    echo "spawned 1 start:" . date("Y-m-d H:i:s") . "\n";
    sleep(1);
    $a += 3;
    var_dump($a);
    echo "spawned 1 end:" . date("Y-m-d H:i:s") . "\n";
});
// 执行任务2
$thread->spawn(function () use (&$a) {
    echo "spawned 2 start:" . date("Y-m-d H:i:s") . "\n";
    sleep(9);
    echo "spawned 2 end:" . date("Y-m-d H:i:s") . "\n";
    $a += 2;
    var_dump($a);
});
// 执行任务3
$thread->spawn(function () use (&$a, $class_a) {
    echo "spawned 3 start:" . date("Y-m-d H:i:s") . "\n";
    sleep(5);
    $a--;
    echo "spawned 3 end:" . date("Y-m-d H:i:s") . "\n";
    $class_a->demo();
    echo "\n";
});

// 主线程
echo "我是主线程\n";

// 等待任务执行完毕
$thread->wait();
var_dump("res:" . $a);
var_dump("end");
